package com.igeek.d0604;

import com.igeek.d0604.exception.DeleteException;
import com.igeek.d0604.exception.InsertException;
import com.igeek.d0604.exception.UpdateException;

import java.util.*;

/**
 * @Author: zhiyu
 * @Date:18:55
 * @DESC:
 */
public class StudentManager {
    public static void main(String[] args) {
        HashMap<String, ArrayList<Student>> map = new HashMap<>();
        // 创建集合对象，用于存储学生数据
        ArrayList<Student> class1 = new ArrayList<>();
        class1.add(new Student("张三", 10010, "男", 23));
        class1.add(new Student("李四", 10011, "男", 23));
        class1.add(new Student("王五", 10012, "男", 23));
        class1.add(new Student("赵六", 10013, "男", 23));
        map.put("一班", class1);
        ArrayList<Student> class2 = new ArrayList<>();
        class2.add(new Student("小红", 10014, "女", 23));
        class2.add(new Student("小芳", 10015, "女", 23));
        class2.add(new Student("小欣", 10016, "女", 23));
        class2.add(new Student("小溪", 10017, "女", 23));
        map.put("二班", class2);
        while (true) {
            //这是学生管理系统的主界面
            System.out.println("--------欢迎来到学生管理系统--------");
            System.out.println("1.查看所有学生");
            System.out.println("2.添加学生信息");
            System.out.println("3.删除学生信息");
            System.out.println("4.修改学生信息");
            System.out.println("5.退出");
            System.out.println("请输入你的选择：");

            //创建键盘录入对象
            Scanner sc = new Scanner(System.in);
            int num = sc.nextInt();
            switch (num) {
                case 1:
                    // 查看所有学生
                    findAllStudent(map);
                    break;
                case 2:
                    // 添加学生
                    addStudent(map);
                    break;
                case 3:
                    // 删除书
                    deleteStudent(map);
                    break;
                case 4:
                    // 修改书
                    editStudent(map);
                    break;
                case 5:
                    // 退出
                    System.out.println("谢谢你的使用");
                    System.exit(0); // JVM退出
                    break;
                default:
                    System.out.println("输入错误,请重新输入");
                    break;
            }
        }
    }
    private static void findAllStudent(Map<String, ArrayList<Student>> map) {
        System.out.println("班级\t\t姓名\t学号\t性别\t年龄");
        Set<Map.Entry<String, ArrayList<Student>>> entries = map.entrySet();
        for (Map.Entry<String, ArrayList<Student>> entry : entries) {
            String key = entry.getKey();
            System.out.println(key);

            ArrayList<Student> value = entry.getValue();
            for (Student student : value) {
                System.out.println("\t\t" + student.getName() + "\t" + student.getStudentId()+ "\t" + student.getSex()+ "\t" + student.getAge());
            }
        }
    }
    private static void addStudent(Map<String, ArrayList<Student>> map){
        try {
            System.out.println("请依次输入你的班级 姓名 学号 性别 年龄（中间用空格隔开）");
            // 创建键盘录入对象
            Scanner scanner = new Scanner(System.in);
            String string = scanner.nextLine();
            String[] split = string.split(" +");
            if (!map.containsKey(split[0])){
                ArrayList<Student> otherClass = new ArrayList<>();
                otherClass.add(new Student(split[1],Integer.parseInt(split[2]),split[3],Integer.parseInt(split[4])));
                map.put(split[0],otherClass);
            }else {
                ArrayList<Student> students = map.get(split[0]);
                int count =0;
                for (int i = 0; i < students.size(); i++) {
                    if (students.get(i).getStudentId().equals(Integer.parseInt(split[2]))){
                        System.out.println("该学号已存在不能重复添加");
                        break;
                    }else {
                        count++;
                    }
                }
                if (count==students.size()){
                    students.add(new Student(split[1],Integer.parseInt(split[2]),split[3],Integer.parseInt(split[4])));
                    System.out.println("添加" + split[1] + "成功");
                }
            }
        }catch (RuntimeException e){
            throw new InsertException("你的添加操作失败啦");
        }
    }
    private static void deleteStudent(Map<String, ArrayList<Student>> map){
        try {
            // 创建键盘录入对象
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要删除学生的班级");
            String className = sc.next();
            System.out.println("请输入要删除的学生学号:");
            String idCard = sc.next();
            if(!map.containsKey(className)){
                System.out.println("该班级不存在");
            }else {
                ArrayList<Student> students = map.get(className);
                int count=0;
                for (int i = 0; i < students.size(); i++) {
                    if(students.get(i).getStudentId().equals(Integer.parseInt(idCard))){
                        students.remove(students.get(i));
                        System.out.println("删除"+idCard+"成功");
                        break;
                    }else {
                        count++;
                    }
                }
                if (count==students.size()-1){
                    System.out.println("没有找到该学生");
                }
            }
        }catch (RuntimeException e){
            throw new DeleteException("你的删除操作失败啦");
        }
    }
    private static void editStudent(Map<String, ArrayList<Student>> map) {
        try {
            // 创建键盘录入对象
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要修改学生的班级:");
            String className = sc.next();
            System.out.println("请输入要修改的学生学号:");
            String idCard = sc.next();
            if(!map.containsKey(className)){
                System.out.println("该班级不存在");
            }else {
                ArrayList<Student> students = map.get(className);
                int count=0;
                for (int i = 0; i < students.size(); i++) {
                    if(students.get(i).getStudentId().equals(Integer.parseInt(idCard))){
                        System.out.println("请输入你要修改的学生年龄");
                        String string = sc.next();
                        students.get(i).setAge(Integer.parseInt(string));
                        System.out.println("年龄修改成功");
                        break;
                    }else {
                        count++;
                    }
                }
                if (count==students.size()){
                    System.out.println("没有找到该学生");
                }
            }
        }catch (RuntimeException e){
            throw  new UpdateException("你的修改操作失败啦");
        }
    }
}